﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace Powershell_ML
{
    public class Mailbox : Exchange_Information 
    {
        public Mailbox()
        {

        }
        /// <summary>
        /// Constructor of Mailbox Type
        /// Based on the PSObject Returned by the requested Mailbox
        /// Once Invoked the command return a PSObject(which is the Mailbox Object Infact..)
        /// A generic Property list is used to manage the object (which include method invoke, for mailbox operation)
        /// </summary>
        /// <param name="Obj"></param>
        public Mailbox(PSObject Obj)
        {
            if (Obj != null)
            {
                try
                {
                    Powershell_ML.Famework.IEntry entry = new Powershell_ML.Famework.Powershell_Utility();
                    this.Property = new List<PSPropertyInfo>();
                    this.Property = entry.Property_Creator(Obj);
                    this.DistinguishedName = entry.Extract_String_Property(this.Property, "DistinguishedName");
                    this.Username = entry.Extract_String_Property(this.Property, "Name");
                    this.Alias = entry.Extract_String_Property(this.Property, "Alias");
                    this.Server = entry.Extract_String_Property(this.Property, "ServerName");
                    this.SendQuota = entry.Extract_String_Property(this.Property, "ProhibitSendQuota");
                    this.LegacyExchangeDN = entry.Extract_String_Property(this.Property, "LegacyExchangeDN");
                    this.Primary_Email = entry.Extract_String_Property(this.Property, "PrimarySmtpAddress");
                    this.Office = entry.Extract_String_Property(this.Property, "Office");
                    this.Database = entry.Extract_String_Property(this.Property, "Database");
                    this.OU = entry.Extract_String_Property(this.Property, "OrganizationalUnit");
                    this.GUID = entry.Extract_String_Property(this.Property, "ExchangeGuid");
                    this.Collected_Date = DateTime.Now;
                    Command Get_Mdb = new Command("Get-MailboxStatistics");
                    Get_Mdb.Parameters.Add("Identity", this.DistinguishedName);
                    PSObject Powershell_Output = Collectorsvc.Program.Runner.Execute_Command_Once(Get_Mdb);
                    this.ItemNumber = Powershell_Output.Properties["ItemCount"].Value.ToString() ?? string.Empty;
                    this.TotalItemSize = Powershell_Output.Properties["TotalItemSize"].Value.ToString();
                    string test = Between(this.TotalItemSize, "(", ")");
                    test= test.Replace("bytes", "");
                    test= test.Replace(" ", "");
                    test =test.Replace(".", "");
                    test = test.Replace(",", "");
                    this.KBSIZE = int.Parse(test);
                    this.StorageLimitStatus = (string)Powershell_Output.Properties["StorageLimitStatus"].Value.ToString() ?? string.Empty;
                    int max = int.Parse(this.ItemNumber);
                    this.Average_Item_Size = 0;
                    if (Powershell_Output.Properties["LastLogonTime"].Value != null)
                    {
                        this.LastLogonTime = (string)Powershell_Output.Properties["LastLogonTime"].Value.ToString() ?? string.Empty;

                    }
                    if (Powershell_Output.Properties["LastLogoffTime"].Value != null || Powershell_Output.Properties["LastLoggedOnUserAccount"].Value.ToString() != null)
                    {
                        this.LastLogOffTime = (string)Powershell_Output.Properties["LastLogoffTime"].Value.ToString() ?? string.Empty;
                        this.LastLogedOnUserAccount = (string)Powershell_Output.Properties["LastLoggedOnUserAccount"].Value.ToString() ?? string.Empty;
                    }
                    this.deleteditemcount = Powershell_Output.Properties["DeletedItemCount"].Value.ToString() ?? string.Empty;
                    if (!this.Mailbox_Has_Stats(this))
                    {
                        this.Mailbox_Export_Stat(this);
                    }
                    else
                    {
                        this.has_stats = true;
                        this.Stats = new List<Mailbox>();
                        this.Stats = this.Mailbox_Load_Stats(this);
                        this.Mailbox_Export_Stat(this);
                    }
                }
                catch (Exception ex) { }

            }
        }
        public int KBSIZE
        {
            get;set;
        }
        public List<Mailbox> Stats
        {
            get;set;
        }
        public int Average_Item_Size
        {
            get;set;
        }
        public string deleteditemcount

        {
            get;set;
        }
        public string LastLogedOnUserAccount
        {
            get;set;
        }
        public string LastLogOffTime
        {
            get;set;
        }
        public string LastLogonTime
        {
            get;set;
        }
        public string StorageLimitStatus
        {
            get;set;
        }
        public string TotalItemSize
        {
            get;set;
        }
        public string ItemNumber
        {
            get;set;
        }
        public DateTime Collected_Date { get; set; }
        [XmlIgnoreAttribute]
        public bool has_stats
        {
            get; set;

        }
        public string GUID
        {
            get;set;
        }

        [XmlIgnoreAttribute]
        /// <summary>
        /// Generic List Hosting all PowerShell PropertyInfo Stocked and return by the Invoker
        /// </summary>
        List<PSPropertyInfo> Property
        {
            get;
            set;
        }
        [XmlIgnoreAttribute]
        public Mailbox_Stats Statistic
        {
            get;
            set;
        }
        [XmlIgnoreAttribute]
        public Mailbox_Permissions Permissions
        {
            get;
            set;
        }
        #region "Property Obj"
        public string Primary_Email
        {
            get;
            set;
        }
        public string Office
        {
            get;
            set;
        }

        public string DistinguishedName
        {
            set;
            get;
        }
        public string Database
        {
            get;
            set;
        }
        public string Username
        {
            get;
            set;
        }
        public string Alias
        {
            get;
            set;
        }
        public string Server
        {
            get;
            set;
        }
        public string SendQuota
        {
            get;
            set;
        }
        public string LegacyExchangeDN
        {
            get;
            set;
        }
        public string OU
        {
            get;
            set;
        }
        #endregion
    }
    public class Mailbox_Stats : Mailbox 
    {
        public Mailbox_Stats(PSObject Object,PSObject Stats):base(Object)
        {
            Powershell_ML.Famework.IEntry entry = Const.entry;
            this.Property = new List<PSPropertyInfo>();
            this.Property = entry.Property_Creator(Stats);
            this.Last_LogOff = Extract_String_Property(this.Property, "LastLogoffTime");
            this.Last_LogOn = Extract_String_Property(this.Property, "LastLogonTime");
            this.Last_Account = Extract_String_Property(this.Property, "LastLoggedOnUserAccount");
            this.Total_Item_Size = Extract_String_Property(this.Property, "TotalItemSize");
        }
        public string Total_Item_Size
        {
            get;
            set;
        }
        public string Last_Account
        {
            get;
            set;
        }
        public string Last_LogOn
        {
            get;
            set;
        }
        public string Last_LogOff
        {
            get;
            set;
        }
        public List<PSPropertyInfo> Property
        {
            get;
            set;
        }
    }
    public class Mailbox_Permissions : Mailbox
    {
        public Mailbox_Permissions(PSObject Object, PSObject Permission) : base(Object)
        {
            Powershell_ML.Famework.IEntry library = new Powershell_ML.Famework.Powershell_Utility();
            this.Property = new List<PSPropertyInfo>();
            this.Property = library.Property_Creator(Permission);
            foreach (PSProperty entry in this.Property)
            {
            }
        }
        public List<PSPropertyInfo> Property
        {
            get;
            set;
        }
    }
}
